class Rules {
    static CARD_TYPES = {
        SINGLE: 'single',
        PAIR: 'pair',
        TRIPLE: 'triple',
        TRIPLE_WITH_ONE: 'triple_with_one',
        TRIPLE_WITH_PAIR: 'triple_with_pair',
        SEQUENCE: 'sequence',
        SEQUENCE_PAIRS: 'sequence_pairs',
        BOMB: 'bomb',
        ROCKET: 'rocket'
    };

    static getCardType(cards) {
        if (!cards || cards.length === 0) return null;
        
        // 先排序
        cards.sort((a, b) => b.value - a.value);
        
        // 火箭（大小王）
        if (this.isRocket(cards)) return this.CARD_TYPES.ROCKET;
        
        // 炸弹
        if (this.isBomb(cards)) return this.CARD_TYPES.BOMB;
        
        // 单张
        if (cards.length === 1) return this.CARD_TYPES.SINGLE;
        
        // 对子
        if (this.isPair(cards)) return this.CARD_TYPES.PAIR;
        
        // 三张
        if (this.isTriple(cards)) return this.CARD_TYPES.TRIPLE;
        
        // 三带一
        if (this.isTripleWithOne(cards)) return this.CARD_TYPES.TRIPLE_WITH_ONE;
        
        // 三带对
        if (this.isTripleWithPair(cards)) return this.CARD_TYPES.TRIPLE_WITH_PAIR;
        
        // 顺子
        if (this.isSequence(cards)) return this.CARD_TYPES.SEQUENCE;
        
        // 连对
        if (this.isSequencePairs(cards)) return this.CARD_TYPES.SEQUENCE_PAIRS;
        
        return null;
    }

    static isRocket(cards) {
        return cards.length === 2 && 
               cards[0].value === 17 && 
               cards[1].value === 16;
    }

    static isBomb(cards) {
        return cards.length === 4 && 
               cards[0].value === cards[1].value && 
               cards[1].value === cards[2].value && 
               cards[2].value === cards[3].value;
    }

    static isPair(cards) {
        return cards.length === 2 && 
               cards[0].value === cards[1].value;
    }

    static isTriple(cards) {
        return cards.length === 3 && 
               cards[0].value === cards[1].value && 
               cards[1].value === cards[2].value;
    }

    static isTripleWithOne(cards) {
        if (cards.length !== 4) return false;
        
        // 检查三张相同的牌
        if (cards[0].value === cards[1].value && 
            cards[1].value === cards[2].value) {
            return true;
        }
        
        if (cards[1].value === cards[2].value && 
            cards[2].value === cards[3].value) {
            return true;
        }
        
        return false;
    }

    static isTripleWithPair(cards) {
        if (cards.length !== 5) return false;
        
        // 检查三张相同的牌和一对
        if (cards[0].value === cards[1].value && 
            cards[1].value === cards[2].value && 
            cards[3].value === cards[4].value) {
            return true;
        }
        
        if (cards[2].value === cards[3].value && 
            cards[3].value === cards[4].value && 
            cards[0].value === cards[1].value) {
            return true;
        }
        
        return false;
    }

    static isSequence(cards) {
        if (cards.length < 5) return false;
        
        // 不能包含2和大小王
        if (cards[0].value >= 15) return false;
        
        // 检查是否连续
        for (let i = 1; i < cards.length; i++) {
            if (cards[i-1].value !== cards[i].value + 1) {
                return false;
            }
        }
        
        return true;
    }

    static isSequencePairs(cards) {
        if (cards.length < 6 || cards.length % 2 !== 0) return false;
        
        // 不能包含2和大小王
        if (cards[0].value >= 15) return false;
        
        // 检查是否都是对子且连续
        for (let i = 0; i < cards.length; i += 2) {
            if (cards[i].value !== cards[i+1].value) return false;
            if (i > 0 && cards[i].value !== cards[i-2].value - 1) return false;
        }
        
        return true;
    }

    static canBeat(newCards, lastCards) {
        if (!lastCards || lastCards.length === 0) return true;
        
        const newType = this.getCardType(newCards);
        const lastType = this.getCardType(lastCards);
        
        // 火箭最大
        if (newType === this.CARD_TYPES.ROCKET) return true;
        if (lastType === this.CARD_TYPES.ROCKET) return false;
        
        // 炸弹可以打任何非火箭牌型
        if (newType === this.CARD_TYPES.BOMB && lastType !== this.CARD_TYPES.BOMB) return true;
        
        // 其他情况必须牌型相同且值更大
        if (newType !== lastType) return false;
        if (newCards.length !== lastCards.length) return false;
        
        return newCards[0].value > lastCards[0].value;
    }
} 